<?php

namespace app\admin\service\recharge;

use app\admin\model\recharge\BoxRechargeGame;
use app\admin\service\BaseService;

class BoxRechargeGameService extends BaseService
{

    public function __construct()
    {
        $this->model = new BoxRechargeGame();
    }

    /**
     * Desc : getList
     * User : Steven
     * Date : 2024-05-28 14:00
     * @return array
     */
    public function getList(): array
    {
        $params = request()->param();
        $page   = $params['page'] ?? 1;
        $limit  = $params['limit'] ?? 15;
        $where  = [];
        $time = $params['time_range'] ?? [];
        $start = $time[0] ?? '';
        $end = $time[1] ?? '';
        if (!empty($start) && !empty($end)) {
            $where[] = ['g.create_time', 'between', [$start, $end]];
        }

        $status = $params['status'] ?? "";
        if (isset($status) && !empty($status)) {
            $where[] = ['g.status', '=', $status];
        }
        if (!empty($params['game_id'])) {
            $where[] = ['g.game_id', '=', $params['game_id']];
        }
        $where[]  = ['g.mark','=',1];
        if(isset($params['game_name'])){
            $where[]  = ['g.status','=',1];
        }

        $model = new BoxRechargeGame();
        $arr   = $model->alias('g')
            ->leftJoin('cq_box_game b', 'b.id = g.game_id')
            ->field('g.*,b.game_name')
            ->where($where)
            ->paginate($limit)
            ->toArray();
        
        return [
            'code'  => 0,
            'msg'   => 'success',
            'count' => $arr['total'],
            'data'  => $arr['data'],
        ];
    }

    public function edit()
    {
        $params = request()->param();
        $game_id = $params['game_id'] ?? 0;
        $id = $params['id'] ?? 0;
        if(empty($params['id'])){
            $result = (new BoxRechargeGame())->where('game_id','=',$game_id)->find();
            if($result){
                return message('该游戏已存在,请勿重复添加', false, $result);
            }
        }
        return parent::newEdit($params,false,true); // TODO: Change the autogenerated stub
    }

    // public function edit()
    // {
    //     $params = request()->param();
    //     unset($params['s']);
    //     $id    = $params['id'] ?? 0;
    //     $game_id = $params['game_id'] ?? 0;
    //     $discount = $params['discount'] ?? 0;
    //     $status = $params['status'] ?? 0;
    //     if(empty($game_id) || empty($discount) || empty($status)){
    //         return message('参数错误', false, $params);
    //     }
    //     $data  = [
    //         'game_id'  => $params['game_id'] ?? 0,
    //         'discount' => $params['discount'] ?? 0,
    //         'status'   => $params['status'] ?? 0,
    //         'rule'     => $params['rule'] ?? '',
    //     ];
    //     $model = new BoxRechargeGame();
    //     if (empty($id)) {
    //         $result = $model->insertGetId($data);
    //         if (!$result) return message('添加失败', false, $result);
    //     } else {
    //         $result = $model->where('id', $id)->update($data);
    //         if (!$result) return message('更新失败', false, $result);
    //     }
    //
    //     return message('success', true, $result);
    // }

    // public function delete(): array
    // {
    //     $params = request()->param();
    //     $id     = $params['id'] ?? [];
    //     if (empty($id)) return message('参数错误', false, $id);
    //     $model  = new BoxRechargeGame();
    //     $result = $model->whereIn('id', $id)->update(['mark' => 0]);
    //     if (!$result) return message('失败', false, $result);
    //     return message('success', true, $result);
    // }
}